<?php
header("Content-type: text/html; charset=utf-8"); 
include_once "DB.class.php";

	// 显示curl 抓取结果
	// echo $is_bad_word;
	
	function get_is_bad_word($str)
	{
		$url = 'http://cn.bing.com/search?q='.urlencode($str);
		$url="https://s.taobao.com/search?q=".urlencode($str);
		$rs  = curl_post($url);
		return $rs;
	}

	function curl_get($f_url){
		//初始化
		$ch = curl_init();
		//设置选项，包括URL
		curl_setopt($ch, CURLOPT_URL, $f_url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		//执行并获取HTML文档内容
		$output = curl_exec($ch);
		//释放curl句柄
		curl_close($ch);

		//返回获得的数据
		return $output;
	}

		 function curl_post($url, $data='', $method='GET'){   
    $curl = curl_init(); // 启动一个CURL会话  
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在  
    // curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器  
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转  
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer  
    if($method=='POST'){  
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求  
        if ($data != ''){  
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包  
        }  
    }  
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回  
    $tmpInfo = curl_exec($curl); // 执行操作  
    curl_close($curl); // 关闭CURL会话  
    return $tmpInfo; // 返回数据   
} 
function preg_A($str){
	$char = "。、！？：；﹑•＂…‘’“”〝〞∕¦‖—　〈〉﹞﹝「」‹›〖〗】【»«』『〕〔》《﹐¸﹕︰﹔！¡？?¿﹖﹌﹏﹋＇´ˊˋ―﹫︳︴¯＿￣﹢﹦﹤‐­˜﹟﹩﹠﹪﹡﹨﹍﹉﹎﹊ˇ︵︶︷︸︹︿﹀︺︽︾ˉ﹁﹂﹃﹄︻︼（），≥";
 
$pattern = array(
    "/[[:punct:]]/i", //英文标点符号
    '/['.$char.']/u', //中文标点符号
    "/\r/",
    "/\n/",
    "/\t/",
    "/ /",
    '/[ ]{2,}/'
);
$str = preg_replace($pattern, '', $str);
return $str;
}
function trimall($str){
    $qian=array(" ","　","\t","\n","\r");
    return str_replace($qian, '', $str);   
}

function mbStrSplit ($string, $len=1) {
  $start = 0;
  $strlen = mb_strlen($string);
  while ($strlen) {
  	$field=mb_substr($string,$start,$len,"utf8");

  	if ($field!="\n" && $field!="\r\n" && $field!="\t" ) {
  		$array[] = $field;
  		# code...
  	}

    $string = mb_substr($string, $len, $strlen,"utf8");
    $strlen = mb_strlen($string);
  }

     return $array;
}


//查询语句插入数据库
function insert_article($content,$pdo){
	$sql="insert into article (content)value('".$content."')";
	$re=$pdo->exec($sql);
}
//取出数据库非法词
function get_arr($sql,$pdo){
	$re=$pdo->query($sql);
	while ($row=$re->fetch(PDO::FETCH_ASSOC)) {
		$arr[]=$row;
		# code...
	}
	return $arr;

}
$content=htmlspecialchars($_POST['content']);
// 从数据库里面取出非法词检查一遍
$str="";
$error_arr=get_arr("select title from illegal",$pdo);
foreach ($error_arr as $k => $v) {
  if ($v!='') {
    $v['title']=trim($v['title']);
    # code...

    	if (strpos($content,$v['title'])!==false) {
           if ($str=='') {
           	$str.=$v['title'];
           	# code...
           }else{
           	$str.=",".$v['title'];
           }
    		# code...
    	}
   }   
	# code...
}

insert_article($content,$pdo);
	if ($str!="") {
		echo json_encode($str) ;exit;
		# code...
	}else{
	echo 0;
	}







